﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Reckoning
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, IView
    {        
        public MainWindow()
        {
            InitializeComponent();
        }   

        public void SetModel(object obj)
        {     
            gridMain.DataContext = obj;
        }

        private void SetTextBoxFile(string file)
        {
            textBoxFile.Text = file;

            BindingExpression be = textBoxFile.GetBindingExpression(
                    TextBox.TextProperty);
            if (be != null)
            {
                be.UpdateSource();
            }            
        }

        private void SetTextBoxContent(string content)
        {
            textBoxContent.Text = content;

            BindingExpression be = textBoxContent.GetBindingExpression(
                    TextBox.TextProperty);
            if (be != null)
            {
                be.UpdateSource();
            }
        }

        private void SetTextBoxReferenceFile(string file)
        {
            textBoxReferenceFile.Text = file;

            BindingExpression be = textBoxReferenceFile.GetBindingExpression(
                    TextBox.TextProperty);
            if (be != null)
            {
                be.UpdateSource();
            }
        }

        private void ButtonFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            // allow .txt files
            dlg.DefaultExt = ".txt";
            dlg.Filter = Properties.Resources.FilterTxt;

            Nullable<bool> ok = dlg.ShowDialog();
            if (ok == true)
            {
                SetTextBoxFile(dlg.FileName);                
            }
        }

        private void ButtonClearContent_Click(object sender, RoutedEventArgs e)
        {
            SetTextBoxContent(string.Empty);
            SetTextBoxFile(string.Empty);
        }               

        private void ButtonReferenceFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            // allow .xml files
            dlg.DefaultExt = ".xml";
            dlg.Filter = Properties.Resources.FilterXml;

            Nullable<bool> ok = dlg.ShowDialog();
            if (ok == true)                            
                SetTextBoxReferenceFile(dlg.FileName);            
        }

        private void DataGridResults_TargetUpdated(object sender, DataTransferEventArgs e)
        {
            if (textBoxContent.Text == string.Empty)
                return;

            expanderResults.IsExpanded = true;
            expanderResults.BringIntoView();
        }       
    }
}
